package com.ymatou.iapi.userservice.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.AddressRequestDtoBean;
import com.ymatou.iapi.userservice.parameter.GetDefaultAddressBean;
import com.ymatou.iapi.userservice.service.GetDefaultAddressCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.sqlwapper.JUserWapper;
/**
*查询默认地址
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetDefaultAddress{
	private static GetDefaultAddressBean getdefaultaddressBean;
	private static GetDefaultAddressCall getdefaultaddressCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询默认地址");
	}

	@Before
	public void caseUp() {
		getdefaultaddressBean=new GetDefaultAddressBean();
		getdefaultaddressCall=new GetDefaultAddressCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getdefaultaddressCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDefaultAddress_001() {
		Logger.start(true,"设置默认地址后查询默认地址成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService
					.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService
					.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result")
					.getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			int userid = urrc.getUserid();
			JUserWapper addressWapper = new JUserWapper();
			Logger.comment("给用户" + userid + "添加3个地址");
			List<AddressRequestDtoBean> addresslist=new ArrayList<AddressRequestDtoBean>();
			for (int i = 0; i < 3; i++) {
				AddressRequestDtoBean  initaddress= JUserCallService.initAddressBean(userid);
				initaddress.setAction("Add");
				String aid = JUserCallService.AddressRequestDtoCall(initaddress);
				initaddress.setiAddressId(Integer.valueOf(aid));
				addresslist.add(initaddress);
			}
			AddressRequestDtoBean def = addresslist.get(0);
			AddressRequestDtoBean  setdefadd=new AddressRequestDtoBean();
			setdefadd.setAction("SetDefault");
			setdefadd.setiAddressId(def.getiAddressId());
			setdefadd.setiUserid(userid);
			JUserCallService.AddressRequestDtoCall(setdefadd);
			
			getdefaultaddressBean.setUserId(userid);
			getdefaultaddressCall.setData(getdefaultaddressBean);
			getdefaultaddressCall.callService();
			Logger.verifyEquals("200",getdefaultaddressCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true",getdefaultaddressCall.getString("Success"), "验证Success");
			JsonObject Result = getdefaultaddressCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(def.getiAddressId(),Result.get("iAddressId").getAsInt(),"验证iAddressId");
			Logger.verifyEquals(1,Result.get("isDefault").getAsInt(),"验证isDefault");
			Logger.verifyEquals("true",Result.get("bIsDefault").getAsString(),"验证bIsDefault");
			Logger.verifyEquals(userid,Result.get("iUserid").getAsInt(),"验证iUserid");
			Logger.verifyEquals(def.getsBaseAddress(),Result.get("sBaseAddress").getAsString(),"验证sBaseAddress");
			Logger.verifyEquals(def.getsDetailAddress(),Result.get("sDetailAddress").getAsString(),"验证sDetailAddress");
			Logger.verifyEquals(def.getsTelphone(),Result.get("sTelphone").getAsString(),"验证sTelphone");
			Logger.verifyEquals(def.getsMobilePhone(),Result.get("sMobilePhone").getAsString(),"验证sMobilePhone");
			Logger.verifyEquals(def.getsRecipient(),Result.get("sRecipient").getAsString(),"验证sRecipient");
			Logger.verifyEquals(def.getsEmail(),Result.get("sEmail").getAsString(),"验证sEmail");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetDefaultAddress_002() {
		Logger.start(true,"不输入userid失败");
		try {
			getdefaultaddressBean.setUserId(null);
			getdefaultaddressCall.setData(getdefaultaddressBean);
			getdefaultaddressCall.callService();
			Logger.verifyEquals("100",getdefaultaddressCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("false",getdefaultaddressCall.getString("Success"), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetDefaultAddress_003() {
		Logger.start(true,"未设置默认地址后查询默认地址成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService
					.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService
					.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result")
					.getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			int userid = urrc.getUserid();
			JUserWapper addressWapper = new JUserWapper();
			Logger.comment("给用户" + userid + "添加3个地址");
			List<AddressRequestDtoBean> addresslist=new ArrayList<AddressRequestDtoBean>();
			for (int i = 0; i < 3; i++) {
				AddressRequestDtoBean  initaddress= JUserCallService.initAddressBean(userid);
				initaddress.setAction("Add");
				String aid = JUserCallService.AddressRequestDtoCall(initaddress);
				initaddress.setiAddressId(Integer.valueOf(aid));
				addresslist.add(initaddress);
				Thread.sleep(1000);
			}
			//如果没设置默认,最后一个是默认
			AddressRequestDtoBean def = addresslist.get(2);
			
			getdefaultaddressBean.setUserId(userid);
			getdefaultaddressCall.setData(getdefaultaddressBean);
			getdefaultaddressCall.callService();
			Logger.verifyEquals("200",getdefaultaddressCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true",getdefaultaddressCall.getString("Success"), "验证Success");
			JsonObject Result = getdefaultaddressCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(def.getiAddressId(),Result.get("iAddressId").getAsInt(),"验证iAddressId");
			Logger.verifyEquals(0,Result.get("isDefault").getAsInt(),"验证isDefault");
			Logger.verifyEquals("false",Result.get("bIsDefault").getAsString(),"验证bIsDefault");
			Logger.verifyEquals(userid,Result.get("iUserid").getAsInt(),"验证iUserid");
			Logger.verifyEquals(def.getsBaseAddress(),Result.get("sBaseAddress").getAsString(),"验证sBaseAddress");
			Logger.verifyEquals(def.getsDetailAddress(),Result.get("sDetailAddress").getAsString(),"验证sDetailAddress");
			Logger.verifyEquals(def.getsTelphone(),Result.get("sTelphone").getAsString(),"验证sTelphone");
			Logger.verifyEquals(def.getsMobilePhone(),Result.get("sMobilePhone").getAsString(),"验证sMobilePhone");
			Logger.verifyEquals(def.getsRecipient(),Result.get("sRecipient").getAsString(),"验证sRecipient");
			Logger.verifyEquals(def.getsEmail(),Result.get("sEmail").getAsString(),"验证sEmail");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetDefaultAddress_004() {
		Logger.start(true,"没有地址返回空");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService
					.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService
					.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result")
					.getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			int userid = urrc.getUserid();
			getdefaultaddressBean.setUserId(userid);
			getdefaultaddressCall.setData(getdefaultaddressBean);
			getdefaultaddressCall.callService();			
			Logger.verifyEquals("200",getdefaultaddressCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true",getdefaultaddressCall.getString("Success"), "验证Success");
			Logger.verifyIsNull(getdefaultaddressCall.getJsonObject().get("Result"), "没有默认地址");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetDefaultAddress_005() {
		Logger.start(true,"地址删除返回空");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService
					.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService
					.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result")
					.getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			int userid = urrc.getUserid();
			
			AddressRequestDtoBean  initaddress= JUserCallService.initAddressBean(userid);
			initaddress.setAction("Add");
			String aid = JUserCallService.AddressRequestDtoCall(initaddress);
			initaddress.setiAddressId(Integer.valueOf(aid));
			
			AddressRequestDtoBean  deleadd=new AddressRequestDtoBean();
			deleadd.setAction("MarkDelete");
			deleadd.setiAddressId(initaddress.getiAddressId());
			deleadd.setiUserid(userid);
			JUserCallService.AddressRequestDtoCall(deleadd);
			
			getdefaultaddressBean.setUserId(userid);
			getdefaultaddressCall.setData(getdefaultaddressBean);
			getdefaultaddressCall.callService();			
			Logger.verifyEquals("200",getdefaultaddressCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true",getdefaultaddressCall.getString("Success"), "验证Success");
			Logger.verifyIsNull(getdefaultaddressCall.getJsonObject().get("Result"), "没有默认地址");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDefaultAddress_006() {
		Logger.start(true,"设置默认地址后删除默认地址查询默认地址成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService
					.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService
					.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result")
					.getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			int userid = urrc.getUserid();
			JUserWapper addressWapper = new JUserWapper();
			Logger.comment("给用户" + userid + "添加3个地址");
			List<AddressRequestDtoBean> addresslist=new ArrayList<AddressRequestDtoBean>();
			for (int i = 0; i < 3; i++) {
				AddressRequestDtoBean  initaddress= JUserCallService.initAddressBean(userid);
				initaddress.setAction("Add");
				String aid = JUserCallService.AddressRequestDtoCall(initaddress);
				initaddress.setiAddressId(Integer.valueOf(aid));
				addresslist.add(initaddress);
				Thread.sleep(1000);
			}
			AddressRequestDtoBean def = addresslist.get(1);
			AddressRequestDtoBean  setdefadd=new AddressRequestDtoBean();
			setdefadd.setAction("SetDefault");
			setdefadd.setiAddressId(def.getiAddressId());
			setdefadd.setiUserid(userid);
			JUserCallService.AddressRequestDtoCall(setdefadd);
			
			setdefadd.setAction("MarkDelete");
			JUserCallService.AddressRequestDtoCall(setdefadd);
			
			def = addresslist.get(2);
			getdefaultaddressBean.setUserId(userid);
			getdefaultaddressCall.setData(getdefaultaddressBean);
			getdefaultaddressCall.callService();
			Logger.verifyEquals("200",getdefaultaddressCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true",getdefaultaddressCall.getString("Success"), "验证Success");
			JsonObject Result = getdefaultaddressCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(def.getiAddressId(),Result.get("iAddressId").getAsInt(),"验证iAddressId");
			Logger.verifyEquals(0,Result.get("isDefault").getAsInt(),"验证isDefault");
			Logger.verifyEquals("false",Result.get("bIsDefault").getAsString(),"验证bIsDefault");
			Logger.verifyEquals(userid,Result.get("iUserid").getAsInt(),"验证iUserid");
			Logger.verifyEquals(def.getsBaseAddress(),Result.get("sBaseAddress").getAsString(),"验证sBaseAddress");
			Logger.verifyEquals(def.getsDetailAddress(),Result.get("sDetailAddress").getAsString(),"验证sDetailAddress");
			Logger.verifyEquals(def.getsTelphone(),Result.get("sTelphone").getAsString(),"验证sTelphone");
			Logger.verifyEquals(def.getsMobilePhone(),Result.get("sMobilePhone").getAsString(),"验证sMobilePhone");
			Logger.verifyEquals(def.getsRecipient(),Result.get("sRecipient").getAsString(),"验证sRecipient");
			Logger.verifyEquals(def.getsEmail(),Result.get("sEmail").getAsString(),"验证sEmail");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}